package saucaca.fragment;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;
import saucaca.CustomView.PagerSlidingTabStrip;
import saucaca.Slidingmenu.SlidingMenu;
import saucaca.Util.TagName;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class CateDetailFragment extends Fragment {
	String TAG = CateDetailFragment.class.getSimpleName();
	MainActivity mActivity;
	ViewPager viewPager;
	OfflineTabPageAdapter TabAdapter;
	String strCateCode;
	private PagerSlidingTabStrip tabs;
	String title;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (MainActivity) getActivity();
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		getActivity().getActionBar().hide();
		View view = inflater.inflate(R.layout.viewpager_tabstrip, container, false);
		findViewById(view);
		TabAdapter = new OfflineTabPageAdapter(getChildFragmentManager());
		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
		viewPager.setPageMargin(pageMargin);
		viewPager.setAdapter(TabAdapter);
		viewPager.setCurrentItem(0);
		tabs.setViewPager(viewPager);
		Bundle b = getArguments();
		String strCate = b.getString(MainActivity.ID);
		if (strCate != null) {
			String[] str = BaseEngine.stringtoArray(strCate, TagName.keyBundle);
			strCateCode = str[0];
			title=str[1];
		}
		mActivity.myActionBar.setMenu(title, false, false);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(title, false, false);
	}
	private void findViewById(View view) {
		viewPager = (ViewPager) view.findViewById(R.id.viewPager);
		tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
		tabs.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int position) {
				switch (position) {
				case 0:
					mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
					break;
				default:
					mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
					break;
				}
			}

		});
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mActivity = (MainActivity) getActivity();
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	public class OfflineTabPageAdapter extends FragmentStatePagerAdapter {
		public static final int PAGE_SONGS = 0;
		public static final int PAGE_VIDEOS = 1;

		public OfflineTabPageAdapter(FragmentManager fm) {
			super(fm);
		}

		String pageTitle[] = { "Bài hát", "Album" , "Video" };

		@Override
		public CharSequence getPageTitle(int position) {
			return pageTitle[position];
		}

		@Override
		public int getCount() {
			return pageTitle.length;
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public Fragment getItem(int i) {
			if (i == 0){
				return new OnlineSongList(strCateCode, "", "", "", "", "", "");
			}else if (i==1) {
				return new AlbumSongList(getActivity(), strCateCode, "", "", "", "", "");
			}
			return new VideoList(strCateCode, "", "", "", "", "", "");
		}
	}
}
